﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentNHibernate.Mapping;
using YasharEl.Infrastructure.Core.EntityModel.Prototype;
using YasharEl.Infrastructure.Core.Prototype;

namespace YasharEl.Infrastructure.Core.PersistenceModel.NHibernate.Prototype.Mappings
{
    public class AbstractPropertyInstanceEntityMap : ClassMap<AbstractPropertyInstanceEntity>
    {
        public AbstractPropertyInstanceEntityMap()
        {
            Table("PropertyInstances");
            Id(e => e.PropertyId)
                .Column("PropertyInstanceId")
                .GeneratedBy
                    .Identity();

            References(e => e.PropertyMapping)
                .Column("PropertyMappingId")
                .Not.Nullable()
                .LazyLoad()
                .Class<AbstractPropertyMappingEntity>()
                .Cascade.All();

            ReferencesAny(e => e.ValueReference)
                .EntityTypeColumn("DataTypeId")
                .EntityIdentifierColumn("PropertyInstanceValueId")
                .IdentityType<long>()
                .AddMetaValue<PropertyInstanceInfinityStringValueEntity>(DataType.String.Value.ToString())
                .AddMetaValue<PropertyInstanceLongValueEntity>(DataType.Long.Value.ToString())
                .AddMetaValue<PropertyInstanceIntegerValueEntity>(DataType.Integer.Value.ToString())
                .AddMetaValue<PropertyInstanceDoubleValueEntity>(DataType.Double.Value.ToString())
                .AddMetaValue<PropertyInstanceDecimalValueEntity>(DataType.Decimal.Value.ToString())
                .AddMetaValue<PropertyInstanceDateTimeValueEntity>(DataType.DateTime.Value.ToString())
                .AddMetaValue<PropertyInstanceBooleanValueEntity>(DataType.Boolean.Value.ToString())
                .AddMetaValue<PropertyInstanceAddressValueEntity>(DataType.Address.Value.ToString())
                .Cascade.All();

            DiscriminateSubClassesOnColumn("")
                .Not.Nullable()
                .Formula(@"(select m.PrototypedObjectType from PropertyMappings m where m.PropertyMappingId = PropertyMappingId)")
                .AlwaysSelectWithValue();
        }
    }
}
